package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.RemoteStrategyEsService;
import cn.wolfcode.wolf2w.business.api.domain.NoteES;
import cn.wolfcode.wolf2w.business.api.domain.StrategyES;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 * 
 * @author zty
 * @date 2025-09-16
 */
@RestController
@RequestMapping("notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private RemoteNoteEsService remoteNoteEsService;
    @RequiresLogin
    @GetMapping("/user/query")
    public R<IPage<Note>> userQuery(NoteQuery qo ) {
        IPage<Note> page = noteService.queryMyNotePage(qo);
        return R.ok(page);
    }
    @GetMapping("/viewnnumTop5")
    public R<List<Note>> viewnnumTop5() {
        List<Note> viewnnumTop5 = noteService.viewnnumTop5();
        return R.ok(viewnnumTop5);
    }

   /*游记详情*/
    @GetMapping("/detail/{id}")
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getcontentById(id);
        return R.ok(note);
    }
    /*查询游记*/
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo ) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }

    /*添加游记*/
    @RequiresLogin
    @PostMapping("/add")
    public R<?> add(@RequestBody Note note) {
        noteService.add(note);
        return R.ok();
    }
    /*es游记初始化*/
    @GetMapping("/dataInit")
    public R<?> dataInit() {
        for (Note note : noteService.list()) {
            NoteES es = new NoteES();
            //同名属性拷贝
            BeanUtils.copyProperties(note, es);
            remoteNoteEsService.initNoteEs(es, SecurityConstants.INNER);


        }
        return R.ok("攻略数据初始化完成");

    }
    /*目的地浏览量前三*/
    @GetMapping("/viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId) {
      List<Note> list =  noteService.viewnnumTop3(destId);
        return R.ok(list);
    }
    /*浏览量自增*/
    @PostMapping("/viewnumIncr")
    public R<Map<String,Object>> viewnumIncr(Long noteId) {
        Map<String,Object> map = noteService.viewnumIncr(noteId);
        return R.ok(map);
    }
    /*游记收藏*/
    @RequiresLogin
    @PostMapping("/collect/{noteId}")
    public R<Map<String,Object>> collect(@PathVariable Long noteId) {
        Map<String,Object> map = noteService.collect(noteId);
        return R.ok(map);
    } /*校验是否是用户已经收藏的*/
    @GetMapping("/isUserFavor")

    public R<Boolean> isUserFavor(Long uid,Long noteId) {
        Boolean b=  noteService.isUserFavor(uid,noteId);
        return R.ok(b);
    }
    @RequiresLogin
    @PostMapping("/star/{noteId}")
    public R< Map<String,Object>> star(@PathVariable Long noteId) {
    Map<String,Object> map =  noteService.star(noteId);
        return R.ok(map);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }
    @GetMapping("/feign/noteStrategyNoteHashPersistence")
    public R<?> noteStrategyNoteHashPersistence(){
        noteService.noteStrategyNoteHashPersistence();
        return R.ok();
    }
}
